package thinking.in.spring.ioc.bean.lifecycle;

import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import thinking.in.spring.ioc.container.overview.domain.User;

import java.util.Properties;

public class BeanMetaDataConfigurationDemo
{
    /**
     * 通过Properties文件注册BeanDefinition
     */
    public static void main(String[] args)
    {
        // TODO

//        PropertiesBeanDefinitionReader propertiesBeanDefinitionReader = new PropertiesBeanDefinitionReader();
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        PropertiesBeanDefinitionReader reader = new PropertiesBeanDefinitionReader(beanFactory);
//        String location = "classpath:/META-INF/User.properties";
        String location = "META-INF/User.properties";
        //指定字符编码
        Resource resource = new ClassPathResource(location);
        EncodedResource encodedResource = new EncodedResource(resource, "utf-8");
        int beanCount = reader.loadBeanDefinitions(encodedResource);
        System.out.printf("成功加载Bean数量：%s", beanCount);
        User user = beanFactory.getBean("user", User.class);
        System.out.println(user.toString());
    }
}
